<?php
class Pageley_Domain_Validator_Composite extends Pageley_Domain_Validator_Abstract {
	
	private $compositeAttribute;
	private $invalidValueMessage = "error.validation.invalidValue";
	
	public function __construct($compositeAttribute) {
		$this->compositeAttribute = $compositeAttribute;
	}
	
	public function validate($value, $propertyName, $object) {
		$errors = new Pageley_Util_ArrayMap();
		if($value !== null && $value !== "") {
            $invalidValue = $this->rejectIfFalse($this->compositeAttribute->isInstance($value), 
    			$this->invalidValueMessage, array("{field." . $propertyName . "}", $value));
    		if($invalidValue) {
    			$errors->put($propertyName, $invalidValue);
    		}
    		try {
    			$this->compositeAttribute->validate($value, $propertyName, $object);
    		} catch(Pageley_Domain_Validator_Exception $e) {
    			$errors->putAll($e->getErrors());
    		}
		}
		return $errors;
	}
	
	public function setInvalidValueMessage($invalidValueMessage) {
		$this->invalidValueMessage = $invalidValueMessage;
	}
	
	
}